﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace UI
{
    public partial class FManageType : Form
    {
        private TypeCollection typList = new TypeCollection();
        public FManageType()
        {
            InitializeComponent();
            typList = FMain.typMan.TypeList;
            try
            {
                gvLoadData();
            }
            catch
            {
            }
        }

        private void gvLoadData()
        {
            DataTable dt = new DataTable();
            DataColumn dc0 = new DataColumn("ID");
            DataColumn dc1 = new DataColumn("Name");
            DataColumn dc2 = new DataColumn("Isincome");
            dt.Columns.Add(dc0);
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            for (int i = 0; i < typList.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = typList[i].ID;
                dr[1] = typList[i].Name;
                if (typList[i].Isincome)
                {
                    dr[2] = "收入";
                }
                else
                {
                    dr[2] = "支出";
                }
                dt.Rows.Add(dr);
            }
                gvType.DataSource = dt;   //说明：gvType的属性为只读，整行选取，每次只能选中一行

        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (gvType.SelectedRows.Count == 0)
            {
                return;
            }
            else
            {
                int rows = gvType.CurrentCell.RowIndex;
                string str = gvType.Rows[rows].Cells[0].Value.ToString();   //获取选中Type的ID
                FCreateType fct = new FCreateType(int.Parse(str));
                fct.Text = "修改收支类型";
                BLL.Type typ = new BLL.Type();
                typ = FMain.typMan.GetType(int.Parse(str));
                fct.tbxTypeName.Text = typ.Name;
                if (typ.Isincome)
                {
                    fct.cbIsincome.SelectedItem = "收入";
                }
                else
                {
                    fct.cbIsincome.SelectedItem = "支出";
                }
                fct.ShowDialog();
                gvLoadData();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection SelRows = gvType.SelectedRows;
            string TypeID;
            for (int i = 0; i < SelRows.Count; i++)
            {
                TypeID = gvType.Rows[SelRows[i].Index].Cells[0].Value.ToString();  //获取被选中的行在gvType中的行数，进而获得被选中的Type的ID
                FMain.typMan.DelType(int.Parse(TypeID));
            }
            gvLoadData();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            FCreateType fct = new FCreateType();
            fct.Text = "新建收支类型";
            fct.ShowDialog();
            gvLoadData();
        }

        private void gvType_MouseDown(object sender, MouseEventArgs e)
        {
            DataGridViewSelectedRowCollection SelRows = gvType.SelectedRows; //由于gvType被设计为每次只能选取一行，故SelRows实际只包含一行 
            if (SelRows[0].Cells[0].Value.ToString() != "")   //如果选中行为数据行，那么SelRows中就应该有数据
            {
                if (e.Button == MouseButtons.Left && e.Clicks > 1)
                {
                    btnModify.PerformClick();
                }

                if (e.Button == MouseButtons.Right)
                {
                    cmsDataGridViewItemRightClick.Show(gvType, e.Location);
                }
            }
        }



        private void tsmiModify_Click(object sender, EventArgs e)
        {
            btnModify.PerformClick();
        }

        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            btnDelete.PerformClick();
        }

        private void tsmiAdd_Click(object sender, EventArgs e)
        {
            btnCreate.PerformClick();
        }

    }
}
